package com.sp.system.user.core.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.MessagingException;
import org.springframework.stereotype.Component;

import javax.jms.Topic;

@Slf4j
@Component
public class TopicProducer {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;
    @Autowired
    private Topic topic;


    public void sendStringTopic(String topicName, String message) {
        try {
            this.jmsMessagingTemplate.convertAndSend(new ActiveMQTopic(topicName), message);
        } catch (MessagingException e) {
            log.error("发送消息失败：{}",e);
        }
        log.debug("发布订阅通讯，topicName={{}}，message：【{}】",topicName,message);
    }

    public void sendByTopic(String msg) {
        // 指定消息发送的目的地及内容
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
        log.debug("发布订阅通讯，message：【{}】",msg);
    }

}
